<template>
    <div class="login-container">
        <div class="login-box">
            <div class="login-header">
                <h2>宿舍管理系统</h2>
                <p>Dormitory Management System</p>
            </div>

            <el-form
                ref="loginFormRef"
                :model="loginForm"
                :rules="rules"
                class="login-form"
                @keyup.enter="handleLogin"
            >
                <el-form-item prop="username">
                    <el-input
                        v-model="loginForm.username"
                        placeholder="请输入用户名"
                        size="large"
                        clearable
                    >
                        <template #prefix>
                            <el-icon><User /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item prop="password">
                    <el-input
                        v-model="loginForm.password"
                        type="password"
                        placeholder="请输入密码"
                        size="large"
                        show-password
                        clearable
                    >
                        <template #prefix>
                            <el-icon><Lock /></el-icon>
                        </template>
                    </el-input>
                </el-form-item>

                <el-form-item>
                    <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
                </el-form-item>

                <el-form-item>
                    <el-button
                        type="primary"
                        size="large"
                        :loading="loading"
                        class="login-button"
                        @click="handleLogin"
                    >
                        登录
                    </el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="background-decoration">
            <div class="circle circle-1"></div>
            <div class="circle circle-2"></div>
            <div class="circle circle-3"></div>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
    username: 'admin',
    password: '',
    remember: false
})

const rules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 3, max: 20, message: '密码长度在 3 到 20 个字符', trigger: 'blur' }
    ]
}

const handleLogin = async () => {
    if (!loginFormRef.value) return

    await loginFormRef.value.validate(async (valid) => {
        if (valid) {
            loading.value = true
            try {
                console.log('🔐 开始登录:', { username: loginForm.username })
                const result = await authStore.login(loginForm.username, loginForm.password)

                console.log('📨 登录结果:', result)

                // 检查登录是否成功
                if (result && result.success === true) {
                    ElMessage.success(result.message || '登录成功！')

                    // 获取重定向地址
                    const redirect = router.currentRoute.value.query.redirect || '/home'

                    console.log('✅ 登录成功，准备跳转到:', redirect)

                    // 延迟一下再跳转，确保 token 已保存
                    setTimeout(() => {
                        router.replace(redirect)
                        console.log('✨ 跳转完成')
                    }, 100)
                } else {
                    console.error('❌ 登录失败，result:', result)
                    ElMessage.error(result?.message || '登录失败，请重试')
                }
            } catch (error) {
                console.error('❌ 登录错误:', error)
                ElMessage.error(error.message || '登录失败，请重试')
            } finally {
                loading.value = false
            }
        }
    })
}
</script>

<style lang="scss" scoped>
.login-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    overflow: hidden;
    margin: 0;
    padding: 0;

    .login-box {
        position: relative;
        z-index: 2;
        width: 420px;
        padding: 40px;
        background: rgba(255, 255, 255, 0.95);
        border-radius: 20px;
        box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
        backdrop-filter: blur(10px);
        animation: slideIn 0.6s ease-out;

        .login-header {
            text-align: center;
            margin-bottom: 40px;

            h2 {
                margin: 0 0 10px 0;
                font-size: 28px;
                font-weight: 600;
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                -webkit-background-clip: text;
                -webkit-text-fill-color: transparent;
                background-clip: text;
                // 兼容不支持 background-clip 的浏览器
                color: #667eea;
            }

            p {
                margin: 0;
                font-size: 14px;
                color: #666;
                font-weight: 300;
                letter-spacing: 0.5px;
            }
        }

        .login-form {
            :deep(.el-form-item) {
                margin-bottom: 24px;

                &:last-child {
                    margin-bottom: 0;
                }
            }

            :deep(.el-input__wrapper) {
                border-radius: 8px;
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
                transition: all 0.3s;

                &:hover {
                    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
                }

                &.is-focus {
                    box-shadow: 0 0 0 1px #667eea inset;
                }
            }

            :deep(.login-button) {
                width: 100%;
                height: 48px;
                border-radius: 8px;
                font-size: 16px;
                font-weight: 600;
                background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
                border: none !important;
                transition: all 0.3s;

                &:hover,
                &:focus {
                    background: linear-gradient(135deg, #7c93f5 0%, #8a5bb8 100%) !important;
                    transform: translateY(-2px);
                    box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);
                }

                &:active {
                    transform: translateY(0);
                    background: linear-gradient(135deg, #5568d3 0%, #63408a 100%) !important;
                }
            }
        }
    }

    .background-decoration {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        overflow: hidden;
        z-index: 1;

        .circle {
            position: absolute;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.1);
            animation: float 20s infinite ease-in-out;

            &.circle-1 {
                width: 300px;
                height: 300px;
                top: -100px;
                left: -100px;
                animation-delay: 0s;
            }

            &.circle-2 {
                width: 500px;
                height: 500px;
                bottom: -200px;
                right: -200px;
                animation-delay: 3s;
            }

            &.circle-3 {
                width: 200px;
                height: 200px;
                top: 50%;
                left: 50%;
                animation-delay: 6s;
            }
        }
    }
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes float {
    0%, 100% {
        transform: translate(0, 0) scale(1);
    }
    33% {
        transform: translate(30px, -50px) scale(1.1);
    }
    66% {
        transform: translate(-20px, 20px) scale(0.9);
    }
}

@media (max-width: 768px) {
    .login-container {
        .login-box {
            width: 90%;
            padding: 30px 20px;
        }
    }
}
</style>

